package window.imp;

import DTO.ExcelConfigDTO;
import handle.ExcelTool;
import handle.imp.DefaultExcelTool;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import util.FrameUtil;
import window.ConfigFrame;
import window.ExcelPasterFrame;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * @description: 文件选择窗口 用于配置excel文件的各种信息
 * @author: ziger
 * @time: 2022/8/28 12:05
 */
public class DefaultConfigFrame extends ConfigFrame {

    private final JButton btnSelectFile = new JButton();
    private final JButton btnFinish = new JButton();

    private final JPanel mainPane = new JPanel();

    private final JPanel topPane = new JPanel();
    private final JPanel centerPane = new JPanel();
    private final JPanel bottomPane = new JPanel();

    private final JPanel beginRowPane = new JPanel();
    private final JPanel endRowPane = new JPanel();
    private final JPanel beginColumPane = new JPanel();
    private final JPanel endColumPane = new JPanel();
    private final JPanel currentRowPane = new JPanel();
    private final JPanel currentColumPane = new JPanel();

    private final JLabel beginRow = new JLabel();
    private final JLabel endRow = new JLabel();
    private final JLabel beginColum = new JLabel();
    private final JLabel endColum = new JLabel();
    private final JLabel currentRow = new JLabel();
    private final JLabel currentColum = new JLabel();
    private final JLabel tip1 = new JLabel();
    private final JLabel tip2 = new JLabel();

    private final JComboBox<Integer> beginRowIndex = new JComboBox<>();
    private final JComboBox<Integer> endRowIndex = new JComboBox<>();
    private final JComboBox<Integer> beginColumIndex = new JComboBox<>();
    private final JComboBox<Integer> endColumIndex = new JComboBox<>();
    private final JComboBox<Integer> currentRowIndex = new JComboBox<>();
    private final JComboBox<Integer> currentColumIndex = new JComboBox<>();

    private File file;
    private JButton btnConfig;

    public DefaultConfigFrame(JButton btnConfig){


        setTitle("文件选择");
        setSize(500,240);
        setResizable(false);
        FrameUtil.center(this);

        setVisible(true);

        this.btnConfig = btnConfig;

        beginRow.setText("开始行");
        endRow.setText("结束行");
        beginColum.setText("开始列");
        endColum.setText("结束列");
        currentRow.setText("当前行");
        currentColum.setText("当前列");


        btnSelectFile.setText("选择文件");
        btnSelectFile.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    btnSelectFile_actionPerformed(e);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (InvalidFormatException ex) {
                    ex.printStackTrace();
                }
            }
        });
        btnFinish.setText("完    成");
        btnFinish.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    btnFinish_actionPerformed(e);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (InvalidFormatException ex) {
                    ex.printStackTrace();
                }
            }
        });
        btnFinish.setEnabled(false);

        tip1.setText("开始与结束位置请不要包含表头,选择纯数据即可");
        tip2.setText("当前行当前列指的是光标开始的位置,选定后复制将从这一格开始");

        mainPane.setLayout(new BorderLayout());

        topPane.setBorder(new EtchedBorder(1));
        centerPane.setBorder(new EtchedBorder(1));
        bottomPane.setBorder(new EtchedBorder(1));

        GridLayout centerLayout = new GridLayout();
        centerLayout.setColumns(2);
        centerLayout.setRows(3);
        centerPane.setLayout(centerLayout);

        FlowLayout flowLayout = new FlowLayout();

        beginRowPane.setLayout(flowLayout);
        endRowPane.setLayout(flowLayout);
        beginColumPane.setLayout(flowLayout);
        endColumPane.setLayout(flowLayout);
        currentRowPane.setLayout(flowLayout);
        currentColumPane.setLayout(flowLayout);

        beginRowPane.add(beginRow);
        beginRowPane.add(beginRowIndex);

        endRowPane.add(endRow);
        endRowPane.add(endRowIndex);

        beginColumPane.add(beginColum);
        beginColumPane.add(beginColumIndex);;

        endColumPane.add(endColum);
        endColumPane.add(endColumIndex);

        currentRowPane.add(currentRow);
        currentRowPane.add(currentRowIndex);

        currentColumPane.add(currentColum);
        currentColumPane.add(currentColumIndex);


        centerPane.add(beginRowPane);
        centerPane.add(endRowPane);
        centerPane.add(beginColumPane);
        centerPane.add(endColumPane);
        centerPane.add(currentRowPane);
        centerPane.add(currentColumPane);

        topPane.add(btnSelectFile);
        topPane.add(btnFinish);

        bottomPane.setLayout(new BoxLayout(bottomPane,BoxLayout.Y_AXIS));
        bottomPane.add(tip1);
        bottomPane.add(tip2);

        mainPane.add(topPane,BorderLayout.NORTH);
        mainPane.add(centerPane,BorderLayout.CENTER);
        mainPane.add(bottomPane,BorderLayout.SOUTH);

        Container container = getContentPane();
        container.add(mainPane);
    }

    private void btnSelectFile_actionPerformed(ActionEvent e) throws IOException, InvalidFormatException {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("表格文件","xls","xlsx"));

        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION){
            //成功选择到文件后将文件信息读取出
            file = fileChooser.getSelectedFile();
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            short lastCellNum = sheet.getRow(0).getLastCellNum();

            //删除所有选项
            beginRowIndex.removeAllItems();
            endRowIndex.removeAllItems();
            currentRowIndex.removeAllItems();
            beginColumIndex.removeAllItems();
            endColumIndex.removeAllItems();
            currentColumIndex.removeAllItems();

            //为combobox添加选项
            for (int i = 0; i <= lastRowNum; i++) {
                beginRowIndex.addItem(i+1);
                endRowIndex.addItem(i+1);
                currentRowIndex.addItem(i+1);
            }

            for (int i = 0; i <= lastCellNum; i++) {
                beginColumIndex.addItem(i+1);
                endColumIndex.addItem(i+1);
                currentColumIndex.addItem(i+1);
            }

            btnFinish.setEnabled(true);

        }
    }

    private void btnFinish_actionPerformed(ActionEvent e) throws IOException, InvalidFormatException {
        ExcelConfigDTO dto = new ExcelConfigDTO(file,
                beginRowIndex.getSelectedIndex(), beginColumIndex.getSelectedIndex(),
                endRowIndex.getSelectedIndex(), endColumIndex.getSelectedIndex(),
                currentRowIndex.getSelectedIndex(), currentColumIndex.getSelectedIndex());
        if (dto.isRight()){

            //选择数据合法
            btnConfig.setEnabled(true);
            ExcelPasterFrame.setTool(new DefaultExcelTool(dto));
            dispose();
        }else {
            JOptionPane.showMessageDialog(this,"数据不合法","错误",JOptionPane.ERROR_MESSAGE);
        }

    }

}
